Skip to content

Method: lambda$create$1(InstanceDescriptor, Object, FieldSpecification)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.sessions.descriptor;
16:
17: import cz.cvut.kbss.jopa.model.LoadState;
18: import cz.cvut.kbss.jopa.model.annotations.FetchType;
19: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
20: import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
21:
22: /**
23: * Builds {@link InstanceDescriptor}s on various occasions.
24: */
25: public class InstanceDescriptorFactory {
26:
27: private InstanceDescriptorFactory() {
28: throw new AssertionError();
29: }
30:
31: /**
32: * Creates an instance descriptor which marks all attributes except the identifier as not loaded.
33: *
34: * @param instance Instance to create descriptor for
35: * @param et Entity type of the instance
36: * @param <T> Instance type
37: * @return Fresh instance descriptor
38: */
39: public static <T> InstanceDescriptor<T> createNotLoaded(T instance, EntityType<T> et) {
40: return new InstanceDescriptor<>(instance, et);
41: }
42:
43: /**
44: * Creates an instance descriptor which marks all attributes as loaded.
45: *
46: * @param instance Instance to create descriptor for
47: * @param et Entity type of the instance
48: * @param <T> Instance type
49: * @return Fresh instance descriptor with all loaded
50: */
51: public static <T> InstanceDescriptor<T> createAllLoaded(T instance, EntityType<T> et) {
52: final InstanceDescriptor<T> descriptor = createNotLoaded(instance, et);
53: et.getFieldSpecifications().forEach(fs -> descriptor.setLoaded(fs, LoadState.LOADED));
54: return descriptor;
55: }
56:
57: /**
58: * Creates an instance descriptor which sets load status of attributes based on their value in the specified
59: * instance as follows:
60: * <p>
61: * If the attribute value is not {@code null}, its status is set to {@link LoadState#LOADED}. If the value is {@code
62: * null} and the attribute fetch type is {@link FetchType#EAGER}, the status is also set to {@code LOADED}.
63: * Otherwise, the status is set to {@link LoadState#UNKNOWN}.
64: *
65: * @param instance Instance to create descriptor for
66: * @param et Entity type of the instance
67: * @param <T> Instance type
68: * @return Fresh instance descriptor
69: */
70: public static <T> InstanceDescriptor<T> create(T instance, EntityType<T> et) {
71: final InstanceDescriptor<T> descriptor = createNotLoaded(instance, et);
72: et.getFieldSpecifications()
73:• .forEach(fs -> descriptor.setLoaded(fs, fs.getFetchType() == FetchType.EAGER ? LoadState.LOADED :
74:• EntityPropertiesUtils.getAttributeValue(fs, instance) != null ?
75: LoadState.LOADED :
76: LoadState.UNKNOWN));
77: return descriptor;
78: }
79:
80: /**
81: * Copies the load states from the specified original descriptor into a new descriptor for the specified instance.
82: *
83: * @param instance Instance to create descriptor for
84: * @param original Load state to copy
85: * @param <T> Instance type
86: * @return Fresh instance descriptor with state copied from the specified one
87: */
88: public static <T> InstanceDescriptor<T> createCopy(T instance, InstanceDescriptor<T> original) {
89: return new InstanceDescriptor<>(instance, original);
90: }
91: }